package org.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.erp.domain.ProcedureDetails;
import org.example.erp.domain.Procedures;
import org.example.erp.domain.Process;
import org.example.erp.domain.Workstation;
import org.example.erp.mapper.ProcessMapper;
import org.example.erp.service.ProceduresDetailsService;
import org.example.erp.service.ProceduresService;
import org.example.erp.service.ProcessService;
import org.example.erp.service.WorkstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 86180
 * @description 针对表【process(工序表)】的数据库操作Service实现
 * @createDate 2024-11-08 15:04:29
 */
@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper, Process>
        implements ProcessService {
    @Autowired
    ProceduresDetailsService proceduresDetailsService;
    @Autowired
    WorkstationService workstationService;

    public Process findDetails(String processCode) {
        return baseMapper.findDetails(processCode);
    }

    @Transactional
    public boolean deleteDetails(List<String> ids) {
        boolean success = true;
        for (String id : ids) {
            QueryWrapper<ProcedureDetails> q1 = new QueryWrapper<>();
            q1.lambda().eq(ProcedureDetails::getProcessCode, id);
            QueryWrapper<Workstation> q2 = new QueryWrapper<>();
            q2.lambda().eq(Workstation::getProcessCode, id);
            if (proceduresDetailsService.count(q1) > 0||workstationService.count(q2)>0) {
                success = false;
            } else {
                QueryWrapper<Process> q3 = new QueryWrapper<>();
                q3.lambda().eq(Process::getProcessCode, id);
                boolean b = removeById(id);
                if (!b) {
                    success = false;
                    break;
                }
            }
        }
        return success;
    }
}




